var errInfo = ['尚未绑定账号,请先绑定账号'];
var _ajax = require('../../../utils/httpUtil.js');
var QQMapWX = require('../../../libs/qqmap-wx-jssdk.js');
var _key = "AFBBZ-26436-QYCSL-ESUEC-SGGWJ-KEBAS";
var qqmapsdk;
qqmapsdk = new QQMapWX({
    key: _key
});
// var app = getApp();
Page({
    data: {
        productInfo: {},
        typeInfo: {},
        typeArr: ['请选择', '美食', '超市'],
        typeIndex: 0,
        address: "请选择",
        shopType: 0,
        userName: "",
        userContact: "",
        userIdCard: "",
        userCardFront: "",
        userCardBack: "",
        shopTitle: "",
        addressProvince: "",
        addressCity: "",
        addressArea: "",
        addressInfo: "",
        longitude: "",
        latitude: "",
        shopLogo: "",
        shopLicense: "",
        userId: "",
        userAccount: "",
        day: '天',
        noneDo: "",
        isPay: '',
        isclick: 0,
        isclickPay: 0
    },
    bindViewEvent: function(e) {
        console.log(e);
        if (e.detail.value == '0') {
            this.setData({
                typeIndex: 0,
                shopType: 0,
                typeInfo: {},
            })
            return;
        }
        this.setData({
            typeIndex: e.detail.value
        })
        showPrice(this);
    },
    //
    onChangeAddress: function(e) {
        var that = this;
        wx.chooseLocation({
            success: function(res) {
                console.log(res);
                // 根据返回的经纬度解析当前地址所在的省市区
                qqmapsdk.geocoder({
                    address: res.address,
                    success: function(data) {
                        console.log(data);
                        that.setData({
                            userRess: {
                                province: data.result.address_components.province,
                                city: data.result.address_components.city,
                                area: data.result.address_components.district,
                                addressInfo: res.name,
                                longitude: res.longitude,
                                latitude: res.latitude,
                            },
                            address: data.result.address_components.province + " " + data.result.address_components.city + " " + data.result.address_components.district,
                            addressInfo: res.name,
                        });
                        console.log(that.data)
                    },
                    fail: function(data) {
                        console.log(data);
                    }
                })
            },
            fail: function(res) {
                console.log(res)
            },
        });
    },
    goToPro: function() {
        //去协议页面
        wx.navigateTo({
            url: "../shopAgreement/shopAgreement"
        });
    },
    inputblur: function(e) {
        console.log(e);
        this.data.userRess.addressInfo = e.detail.value;
        console.log(this.data.userRess)
    },
    copyUrl: function(e) {
        wx.setClipboardData({
            data: e.currentTarget.dataset.copyurl,
            success(res) {
                wx.getClipboardData({
                    success(res) {
                        console.log(res.data) // data
                    }
                })
            }
        });
    },
    copyUrl2: function(e) {
        wx.setClipboardData({
            data: e.currentTarget.dataset.copyurl,
            success(res) {
                wx.getClipboardData({
                    success(res) {
                        // console.log(res.data) // data
                        // wx.showToast({
                        // 	title: '连接复制成功,请前往浏览器下载',
                        // 	icon:'none',
                        // 	duration: 2000
                        // })
                    }
                })
            }
        });
    },
    formSubmit: function(e) {
        console.log(e.detail.value);
        let value = e.detail.value;
        const that = this;
        console.log(that.data.isclick);
        if (that.data.isclick == 1) {
            return;
        }
        if (that.data.isclick == 0) {
            that.setData({
                isclick: 1
            });
            if (value.userName == "" || value.userContact == "" || value.userIdCard == "" || value.shopTitle == "" || value.addressInfo == "") {
                wx.showToast({
                    title: '请完善表单信息',
                    icon: 'none',
                    duration: 2000
                });
                that.setData({
                    isclick: 0
                });
                return;
            };
            if (value.userIdCard.length < 15 || value.userIdCard.length > 18) {
                wx.showToast({
                    title: '身份证为15~18个字符',
                    icon: 'none',
                    duration: 2000
                });
                that.setData({
                    isclick: 0
                });
                return;
            }
            if (!that.data.userRess) {
                wx.showToast({
                    title: '请填写门店区域',
                    icon: 'none',
                    duration: 2000
                });
                that.setData({
                    isclick: 0
                });
                return;
            };
            if (!that.data.userCardFront || !that.data.userCardBack || !that.data.shopLicense || !that.data.shopLogo) {
                wx.showToast({
                    title: '请上传所需照片',
                    icon: 'none',
                    duration: 2000
                });
                that.setData({
                    isclick: 0
                });
                return;
            };
            if (value.shopType == '0') {
                wx.showToast({
                    title: '请选择门店类型',
                    icon: 'none',
                    duration: 2000
                });
                that.setData({
                    isclick: 0
                });
                return;
            };

            console.log(this.data)
            let param = {
                userName: value.userName,
                userContact: value.userContact,
                userIdCard: value.userIdCard,
                userCardFront: that.data.userCardFront,
                userCardBack: that.data.userCardBack,
                shopTitle: value.shopTitle,
                addressProvince: that.data.userRess.province,
                addressCity: that.data.userRess.city,
                addressArea: that.data.userRess.area,
                addressInfo: value.addressInfo,
                longitude: that.data.userRess.longitude,
                latitude: that.data.userRess.latitude,
                shopType: value.shopType,
                shopLogo: that.data.shopLogo,
                userIdCard: value.userIdCard,
                shopLicense: that.data.shopLicense,
                userId: value.userId,
                userAccount: value.userContact,
            };
            console.log(param);
            _ajax.reqPost('xshop/addShopInfo.do', param, function(datares) {
                console.log(datares);
                if (datares.resultCode == 1000) {
                    //  that.setData({
                    //     isclick: 0
                    //   });
                    updata(that, that.data.userId);
                } else if (datares.resultCode == 1008) {
                    that.setData({
                        isclick: 0
                    });
                    wx.showToast({
                        title: datares.returnObject,
                        icon: 'none',
                        duration: 2000
                    });
                } else {
                    that.setData({
                        isclick: 0
                    });
                    wx.showToast({
                        title: '提交失败',
                        icon: 'none',
                        duration: 2000
                    });
                }
            });
        }
    },
    uploadPics: function(e) {
        console.log(e);
        var tt = e.currentTarget.dataset.img;
        var ul = uploadPic(this, tt);
    },
    // 支付订单
    payFormSubmit: function(e) {
        console.log(e);
        var that = this;
        if (that.data.isclickPay == 0) {
            that.setData({
                isclickPay: 1
            });
            _ajax.reqPost('xshop/payAddShopInfo', {
                userId: that.data.userId
            }, function(data) {
                if (data.resultCode == 1000) {
                    wx.requestPayment({
                        timeStamp: data.returnObject.timestamp,
                        nonceStr: data.returnObject.noncestr,
                        package: data.returnObject.wxpackage,
                        signType: 'MD5',
                        paySign: data.returnObject.paySign,
                        success(res) {
                            console.log(res);
                            that.setData({
                                isclickPay: 0
                            });
                            updata(that, that.data.userId)
                        },
                        fail(res) {
                            that.setData({
                                isclickPay: 0
                            });
                            wx.showToast({
                                title: '支付失败',
                                icon: 'fail',
                                duration: 2000
                            });
                        }
                    })
                } else {
                    that.setData({
                        isclickPay: 0
                    });
                    wx.showToast({
                        title: '订单获取失败',
                        icon: 'fail',
                        duration: 2000
                    });
                }
            })
        }
    },
    refuseFormSubmit: function(e) {
        // console.log(e);
        this.setData({
            noneDo: 0
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var _this = this;
        wx.getStorage({
            key: 'uid',
            success: function(res) {
                console.log(res)
                _this.setData({
                    userId: res.data
                });
                updata(_this, res.data);
            },
        });

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        // console.log(this.data);
        // if (this.data.userRess) {
        //   const province = this.data.userRess.province;
        //   const city = this.data.userRess.city;
        //   const area = this.data.userRess.area;
        //   const addressInfo = this.data.userRess.addressInfo
        //   this.setData({
        //     address: province + " " + city + " " + area,
        //     addressInfo: addressInfo
        //   });
        // }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        console.log("用户下拉了动作");
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
});

function onLoadDo(_this) {
    console.log(_this);
    showPrice(_this); //显示价格和时长
}

function showPrice(_this) {
    var _type = _this.data.typeIndex;
    if (_type == 0) {
        _this.setData({
            typeInfo: {}
        });
        return;
    }
    _ajax.reqPost('xshop/getTimePSet', {
        type: _type
    }, function(res) {
        console.log(res);
        if (res.resultCode == 1000) {
            var dd = res.returnObject;
            if (dd.price == 0) {
                dd.time;
                _this.setData({
                    day: '天'
                });
            } else {
                _this.setData({
                    day: '年'
                });
            }
            dd.price = dd.price / 100;
            _this.setData({
                typeInfo: dd,
                shopType: _type
            });
        }
    });
}

function uploadPic(that, tt) {
    console.log(that);
    console.log(tt);

    //添加Banner  
    wx.chooseImage({
        count: 3, //最多可以选择的图片总数  
        sizeType: ['original'], // 可以指定是原图还是压缩图，默认二者都有  
        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有  
        success: function(res) {
            console.log(that);
            // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片  
            var tempFilePaths = res.tempFilePaths;
            //启动上传等待中...  
            wx.showToast({
                title: '正在上传...',
                icon: 'loading',
                mask: true,
                duration: 10000
            })
            var uploadImgCount = 0;
            for (var i = 0, h = tempFilePaths.length; i < h; i++) {
                wx.uploadFile({
                    url: _ajax.path + 'sys/upload/uploadPics',
                    filePath: tempFilePaths[i],
                    name: 'file',
                    formData: {
                        'imgIndex': i
                    },
                    header: {
                        "Content-Type": "multipart/form-data"
                    },
                    success: function(res) {
                        console.log(res);
                        res = JSON.parse(res.data);
                        uploadImgCount++;
                        //如果是最后一张,则隐藏等待中  
                        if (uploadImgCount == tempFilePaths.length) {
                            wx.hideToast();
                        }
                        var ul = res.returnObject;
                        callBackUpload(that, tt, ul);
                    },
                    fail: function(res) {
                        wx.hideToast();
                        wx.showModal({
                            title: '错误提示',
                            content: '上传图片失败',
                            showCancel: false,
                            success: function(res) {
                                console.log(res)
                            }
                        })
                    }
                });
            }
        }
    });
}
//回显图片
function callBackUpload(_this, tt, ul) {
    var ds = {};
    console.log(ul);
    if (tt == 1) {
        ds.userCardFront = ul;
    } else if (tt == 2) {
        ds.userCardBack = ul;
    } else if (tt == 3) {
        ds.shopLicense = ul;
    } else if (tt == 4) {
        ds.shopLogo = ul;
    };
    console.log(_this.data);
    _this.setData(ds);
    console.log(_this.data);
}

function updata(_this, res) {
	_this.setData({
		noneDo: 0,
	});
    _ajax.reqPost('xshop/getShopByWxUserId', {
        wxUserId: res
    }, function(res) {
        console.log(res);
        // res.returnObject.shopStatus = 3
        if (res.returnObject == null) {
            // _this.setData({
            //     noneDo: 0,
            // });
        } else {
            if (res.returnObject.shopStatus) {
                if (res.returnObject.shopStatus == 4) {
                    // 审核中
                    res.returnObject.shopType = res.returnObject.shopType == 1 ? '美食' : '超市';
                    res.returnObject.createTime = res.returnObject.createTime.split(" ")[0];
                    _this.setData({
                        noneDo: 1,
                        isPay: 1,
                        waiting: res.returnObject
                    });

                } else if (res.returnObject.shopStatus < 4 && res.returnObject.shopStatus > 0) {
                    console.log(1111)
                    // 通过审核
                    _this.setData({
                        noneDo: 2,
                        adopt: res.returnObject
                    })
                } else if (res.returnObject.shopStatus == 6) {
                    // 拒绝
                    res.returnObject.shopType = res.returnObject.shopType == 1 ? '美食' : '超市';
                    _this.setData({
                        noneDo: 3,
                        typeIndex: 0,
                        address: res.returnObject.addressProvince + " " + res.returnObject.addressCity + " " + res.returnObject.addressArea,
                        shopType: res.returnObject.shopType,
                        userName: res.returnObject.userName,
                        userContact: res.returnObject.userContact,
                        userIdCard: res.returnObject.userIdCard,
                        userCardFront: res.returnObject.userCardFront,
                        userCardBack: res.returnObject.userCardBack,
                        shopTitle: res.returnObject.shopTitle,
                        addressProvince: res.returnObject.addressProvince,
                        addressCity: res.returnObject.addressCity,
                        addressArea: res.returnObject.addressArea,
                        addressInfo: res.returnObject.addressInfo,
                        longitude: res.returnObject.longitude,
                        latitude: res.returnObject.latitude,
                        shopLogo: res.returnObject.shopLogo,
                        shopLicense: res.returnObject.shopLicense,
                        userId: res.returnObject.userId,
                        userAccount: res.returnObject.userAccount,
                        mark: res.returnObject.mark
                    })
                } else if (res.returnObject.shopStatus == 7) {
                    // 待支付
                    res.returnObject.shopType = res.returnObject.shopType == 1 ? '美食' : '超市';
                    _this.setData({
                        isPay: 2,
                        noneDo: '1',
                        waiting: res.returnObject,

                    });
                }
            }
        }
    });
};
// function getLocalPosi(that,address){

// }